#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/platform_device.h>
#include <linux/of_gpio.h>
#include <linux/timer.h>
#include <linux/spinlock.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>

/*添加内核定时器实现LED闪烁*/

#define LED_CNT				1		  		/* 设备号个数 */
#define LED_NAME		"smartled"			/* 名字 */
#define LEDOFF 				0				/* 关灯 */
#define LEDON 				1				/* 开灯 */
#define CLOSE_CMD 		(_IO(0XEF, 0x1)) 	/* 关闭定时器 */
#define OPEN_CMD 		(_IO(0XEF, 0x2)) 	/* 打开定时器 */
#define SETPERIOD_CMD 	(_IO(0XEF, 0x3)) 	/* 设置定时器周期命令 */


/* smartled设备结构体 */
struct smartled_dev{
	dev_t devid;				/* 设备号 	 */
	struct cdev cdev;			/* cdev 	*/
	struct class *class;		/* 类 		*/
	struct device *device;		/* 设备 	 */
	int major;					/* 主设备号	  */
	int minor;					/* 次设备号   */
	struct device_node	*nd; 	/* 设备节点 */
	int led_gpio;				/* led所使用的GPIO编号*/
	struct timer_list timer; 	/* 定义定时器 */
	unsigned int timeperiod;              
	spinlock_t lock; 			/* 定义自旋锁 */
};

struct smartled_dev smartled;	/* led设备 */

static int led_open(struct inode *inode, struct file *filp)
{
	filp->private_data = &smartled; /* 设置私有数据 */
	return 0;
}

static ssize_t led_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{
	return 0;
}

static ssize_t led_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
	int retvalue;
	unsigned char databuf[1];
	unsigned char ledstat;
	struct smartled_dev *dev = filp->private_data;

	retvalue = copy_from_user(databuf, buf, cnt);

	if(retvalue < 0) {
		printk("kernel write failed!\r\n");
		return -EFAULT;
	}

	ledstat = databuf[0];		/* 获取状态值 */

	if(ledstat == LEDON) {	
		gpio_set_value(dev->led_gpio, 0);	/* 打开LED灯 */
	} else if(ledstat == LEDOFF) {
		gpio_set_value(dev->led_gpio, 1);	/* 关闭LED灯 */
	}
	return 0;
}

static int led_release(struct inode *inode, struct file *filp)
{
	return 0;
}
/*
*	定时器设置函数
*	cmd： 关闭、打开、设置周期
*	arg:  定时器周期
*/

static long led_unlocked_ioctl (struct file *filp, unsigned int cmd, unsigned long arg)
{
	struct smartled_dev *dev = filp->private_data;
	unsigned long flags;
	switch (cmd)
	{
	case CLOSE_CMD:
		/* code */
		del_timer_sync(&dev->timer);
		break;
	case OPEN_CMD:
		/* code */
		mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->timeperiod));  
		break;
	case SETPERIOD_CMD:
		/* code */
		spin_lock_irqsave(&dev->lock, flags);
		dev->timeperiod = arg;
		spin_unlock_irqrestore(&dev->lock, flags);
		mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->timeperiod));
		break;
	default:
		break;
	}
	return 0;
}
/* 设备操作函数 */
static struct file_operations smartled_fops = {
	.owner = THIS_MODULE,
	.open = led_open,
	.read = led_read,
	.write = led_write,
	.release = 	led_release,
	.unlocked_ioctl = led_unlocked_ioctl,
};

void timer_function(unsigned long arg)
{
	struct smartled_dev *dev = (struct smartled_dev *)arg;
	static int led_state = 1;

	led_state =!led_state;

	gpio_set_value(dev->led_gpio, led_state);       /*LED取反*/
	
	mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->timeperiod));   /*重新设置定时器周期*/
}
static int led_probe(struct platform_device *dev)
{
    int ret;

	spin_lock_init(&smartled.lock);
	/* 注册字符设备驱动 */
	/* 1、创建设备号 */
	if (smartled.major) {		/*  定义了设备号 */
		smartled.devid = MKDEV(smartled.major, 0);
		register_chrdev_region(smartled.devid, LED_CNT, LED_NAME);
	} 
    else {						/* 没有定义设备号 */
		alloc_chrdev_region(&smartled.devid, 0, LED_CNT, LED_NAME);	/* 申请设备号 */
		smartled.major = MAJOR(smartled.devid);	/* 获取分配号的主设备号 */
		smartled.minor = MINOR(smartled.devid);	/* 获取分配号的次设备号 */
	}
	printk("smartled major=%d,minor=%d\r\n",smartled.major, smartled.minor);	
	
	/* 2、初始化cdev */
	smartled.cdev.owner = THIS_MODULE;
	cdev_init(&smartled.cdev, &smartled_fops);
	
	/* 3、添加一个cdev */
	cdev_add(&smartled.cdev, smartled.devid, LED_CNT);

	/* 4、创建类 */
	smartled.class = class_create(THIS_MODULE, LED_NAME);
	if (IS_ERR(smartled.class)) {
		return PTR_ERR(smartled.class);
	}

	/* 5、创建设备 */
	smartled.device = device_create(smartled.class, NULL, smartled.devid, NULL, LED_NAME);
	if (IS_ERR(smartled.device)) {
		return PTR_ERR(smartled.device);
	}

	/* 设置LED所使用的GPIO */
	/* 1、获取设备节点：smartled */
	smartled.nd = of_find_node_by_path("/smartled");
	if(smartled.nd == NULL) {
		printk("smartled node not find!\r\n");
		return -EINVAL;
	} else {
		printk("smartled node find!\r\n");
	}

	/* 2、 获取设备树中的gpio属性，得到LED所使用的LED编号 */
	smartled.led_gpio = of_get_named_gpio(smartled.nd, "led-gpio", 0);
	if(smartled.led_gpio < 0) {
		printk("can't get led-gpio");
		return -EINVAL;
	}
	printk("led-gpio num = %d\r\n", smartled.led_gpio);

	/* 3、设置GPIO1_IO03为输出，并且输出高电平，默认关闭LED灯 */

    gpio_request(smartled.led_gpio, "led0");
	ret = gpio_direction_output(smartled.led_gpio, 1);
	if(ret < 0) {
		printk("can't set gpio!\r\n");
	}
	/*初始化定时器,未注册不会启动*/
	init_timer(&smartled.timer);
	smartled.timer.function = timer_function;
	smartled.timeperiod     = 500;       /*默认周期为500ms*/
	smartled.timer.data     = (unsigned long)&smartled;
	return 0;
}
static int led_remove(struct platform_device *dev)
{
	gpio_set_value(smartled.led_gpio, 1); 

	del_timer_sync(&smartled.timer);        /*删除timer*/

	cdev_del(&smartled.cdev);/*  删除cdev */
	unregister_chrdev_region(smartled.devid, LED_CNT); /* 注销设备号 */

	device_destroy(smartled.class, smartled.devid);
	class_destroy(smartled.class);
	return 0;
}

static const struct of_device_id led_of_match[] = {   /*设备树匹配表*/
    {.compatible = "smart-led"},
    {},                                /*最后一项需为空*/
};
/*platform 驱动结构体*/
static struct platform_driver led_driver = {
    .driver = {
        .name = "imx6ul-led",                 /*驱动和设备名一致时，相互匹配*/
        .of_match_table = led_of_match,       /*设备树匹配*/
    },
    .probe  =  led_probe,
    .remove =  led_remove,
};
static int __init led_init(void)
{
	return platform_driver_register(&led_driver);
}

static void __exit led_exit(void)
{
    platform_driver_unregister(&led_driver);
}

module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("destiny");
